﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Serialization;
using System.ComponentModel;

namespace XEvents.Library
{
    public abstract class XEColumnBase : XEventBase
    {
        internal string name;
        internal int column_id;
        internal string object_name;
        internal Guid object_package_guid;
        internal string type_name;
        internal Guid type_package_guid;
        internal string column_type;
        internal string column_value;
        internal int capabilities;
        internal string capabilities_desc;
        internal string description;

        /// <summary>
        /// Mandatory Column (required for Target specification)
        /// </summary>
        [DescriptionAttribute("Mandatory Column (required for Target specification)")]
        [XmlAttribute("Required")]
        public bool Required
        {
            get
            {
                switch (this.capabilities_desc)
                {
                    case "mandatory":
                        return true;
                    default:
                        return false;
                }
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// The Type of data this Column exposes
        /// </summary>
        //[XmlAttribute("Type")]
        [DescriptionAttribute("The Type of data this Column exposes")]
        [XmlIgnore()]
        public Type Type
        {
            get
            {
                return (from t in Server.MetaData.Types
                       where t.name == this.type_name && t.package_guid == this.type_package_guid
                       select t).ElementAtOrDefault(0);
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// Valid Values Mapped to the Column Type
        /// </summary>
        public IEnumerable<MapValue> TypeMapping
        {
            get
            {
                return new MapValueCollection(from m in Server.MetaData.Maps
                       where m.name == this.type_name 
                       && m.package_guid == this.type_package_guid
                       from mv in m.Values
                       select mv);
            }
        }

        /// <summary>
        /// Column Name
        /// </summary>
        [DescriptionAttribute("Column Name")]
        [XmlAttribute("Name")]
        public string Name
        {
            get { return name; }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// Column ID
        /// </summary>
        [DescriptionAttribute("Column ID")]
        [XmlAttribute("ColumnID")]
        public int ColumnID
        {
            get { return column_id; }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// Column Capabilities
        /// </summary>
        [DescriptionAttribute("Column Capabilities")]
        [XmlAttribute("Capabilities")]
        public int Capabilities
        {
            get
            {
                return capabilities;
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// Column Capabilities Description
        /// </summary>
        [DescriptionAttribute("Column Capabilities Description")]
        [XmlAttribute("Capabilities_Desc")]
        public string Capabilities_Desc
        {
            get
            {
                return capabilities_desc;
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// Column Description
        /// </summary>
        [DescriptionAttribute("Column Description")]
        [XmlAttribute("Description")]
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        internal XEColumnBase(SqlServer server)
            : base(server)
        { }
        internal XEColumnBase(dm_xe_object_column col, SqlServer server)
            : base(server)
        {
            this.name = col.name;
            this.column_id = col.column_id;
            this.object_name = col.object_name;
            this.object_package_guid = col.object_package_guid;
            this.type_name = col.type_name;
            this.type_package_guid = col.type_package_guid;
            this.column_type = col.column_type;
            this.column_value = col.column_value;
            Int32.TryParse(col.capabilities.ToString(), out this.capabilities);
            this.capabilities_desc = col.capabilities_desc;
            this.description = col.description;
        }
    }
}
